#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :attendance.py
# Time  :2025/4/29 16:20
# Email :fortunatedong@163.com
# Author:by Adonli
# 文件路径设置
import json
import os
from datetime import datetime

from config import ROOT_DIR

current_attendance_dir = os.path.join(ROOT_DIR, 'Datas/hr_attendance')
current_staff_sort_path = f"{current_attendance_dir}/department_staff_sort.json"
os.makedirs(current_attendance_dir, exist_ok=True)


# 加载/初始化数据
def load_current_staff_sort():

    try:
        with open(current_staff_sort_path, 'r', encoding="utf-8") as f:
            return json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        default_data = [{"department": "人力资源部","sort": 1.0,"staff": [{"name": "冷虎军","sort": 1.0,"lastmonth_yearholiday": 1.0}]},]
        with open(current_staff_sort_path, 'w', encoding="utf-8") as f:
            json.dump(default_data, f, ensure_ascii=False, indent=2)
        return default_data


# 保存数据
def save_current_staff_sort(data):
    with open(current_staff_sort_path, 'w', encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)

def sort_by_department_asc(data):
    """按照sort字段降序排序"""
    return sorted(data, key=lambda x: x["sort"], reverse=False)
def sort_by_staff_asc(data):
    """按照sort字段降序排序"""
    return sorted(data, key=lambda x: min(s["sort"] for s in x["staff"]) if x["staff"] else float('inf'), reverse=False)

